import loader from "../Loader";

class SceneManager extends Laya.Box {
    constructor () {
        super();
        this._curScene = null;
    }

    init () {
        this.zOrder = 0;
        Laya.stage.addChild(this);
    }

    _onResize () {
        const {width, height} = Laya.stage;
        this.size(width, height);
    }

    open (urlOrClass, params) {
        switch (typeof urlOrClass) {
            case "string":
                loader.load("Scene", urlOrClass, Laya.Handler.create(this, this.switchScene, [params]));
                break;
            case "function":
                this.switchScene(params, new urlOrClass)
                break;
        }
    }

    switchScene (params, scene) {
        scene.onOpened && scene.onOpened(params);
        this.addChild(scene);

        if(this._curScene){
            this._curScene.onClosed && this._curScene.onClosed();
            this._curScene.destroy();
        }
        this._curScene = scene;
    }

    get runningScene () {
        return this._curScene;
    }
}

export default new SceneManager;